package application;

import java.util.ArrayList;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class Settings {
	
	public String homePath;
	public String lastStory;
	private static String home = System.getProperty("user.home").toString();
	public ArrayList<String> stories;
	
	public boolean useDefaultEditor;
	public String customEditor;
	
	public boolean useDefaultFiletype;
	public String customFileType;
	
	public boolean useDefaultWebBrowser;
	public String customWebBrowser;
	
	public boolean useDefaultLookandFeel;
	public boolean customLookandFeel;
	
	public boolean startMaximized;
	
	public boolean useDefaultFont;
	public String customFont;
	
	public Settings(String homePath,String lastStory, ArrayList<String> stories){
		super();
		this.homePath = homePath;
		this.lastStory = lastStory;
		this.stories = stories;
	}
	
	public Settings(){
		super();
		this.homePath = System.getProperty("user.home").toString();
		this.lastStory = null;
		this.stories = new ArrayList<String>();
		this.useDefaultEditor = true;
		this.useDefaultFiletype = true;
		this.useDefaultFont = true;
		this.useDefaultLookandFeel = true;
		this.useDefaultWebBrowser = true;
	}

	public String getHomePath() {
		return homePath;
	}

	public String getLastStory() {
		return lastStory;
	}

	public void setHomePath(String homePath) {
		this.homePath = homePath;
	}

	public void setLastStory(String lastStory) {
		this.lastStory = lastStory;
	}

	public void dbstore (String story){
		
		String file = home + "/.storyLine/" + "StoryLine.conf";
		
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), file);
		
		try{

	        		db.store(this);


		}
		finally{
			db.close();
		}
	}

	
		
		public void updateStory (String story){
			
			String file = home + "/.storyLine/" + "StoryLine.conf";
			
			ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
			        .newConfiguration(), file);
			
			try{
				try{
					ObjectSet<Settings> result = db
			        .queryByExample(Settings.class);
					Settings found = (Settings) result.next();
					db.delete(found);
					this.setLastStory(story);

					db.store(this);
					
				}
				catch(IllegalStateException e){
					
				}

			}
			finally{
				db.close();
			}
		
	}
		
		public void update (Settings s){
			
			String file = home + "/.storyLine/" + "StoryLine.conf";
			
			ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
			        .newConfiguration(), file);
			
			try{
				try{
					ObjectSet<Settings> result = db
			        .queryByExample(Settings.class);
					Settings found = (Settings) result.next();
					db.delete(found);
					this.setLastStory(s.getLastStory());
					this.setStories(s.getStories());

					db.store(this);
					
				}
				catch(IllegalStateException e){
					
				}

			}
			finally{
				db.close();
			}
		
	}


		public ArrayList<String> getStories() {
			return stories;
		}

		public void setStories(ArrayList<String> stories) {
			this.stories = stories;
		}
}


